package net.sinodata.smartframework.schedulejob.job;

import net.sinodata.smartframework.schedulejob.base.AbstractScheduleJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description: Test01定时任务
 * 
 * @author: yinliangyun
 * @date: 2021-5-18
 */
public class Test01Job extends AbstractScheduleJob {
	//
	private static final Logger logger = LoggerFactory.getLogger(Test01Job.class);

	/**
 * 执行定时任务的方法
 *
 * @param context JobExecutionContext对象，包含执行上下文信息
 * @throws JobExecutionException 如果执行过程中出现异常
 */
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

    // 模拟任务执行前的延迟
    try {
        Thread.sleep(30000);
        logger.info("Test01Job execute sleep start");
    } catch (InterruptedException e) {
        logger.error("Test01Job execute InterruptedException" + "_" + e.getMessage(), e);
    }

    // 获取是否允许多个实例同时运行的标志，默认为true，
    Boolean multiRunFlag = (Boolean) context.getJobDetail().getJobDataMap().get("multiRunFlag");
    
    // 调用父类AbstractScheduleJob中的isNeedWork方法，判断当前节点是否需要执行任务
    // 该方法基于Redisson分布式锁和心跳机制实现，用于在多机部署环境下确保同一时间只有一个节点执行任务
    Boolean isNeedWork = isNeedWork();
    logger.info("Test01Job execute multiRunFlag : {}, isNeedWork : {}", multiRunFlag, isNeedWork);

    // 如果不允许同时运行多个实例且当前实例不需要工作，则直接返回
    if (!multiRunFlag && !isNeedWork) {
        return;

    }

    // 模拟任务执行中的延迟
    try {
        Thread.sleep(30000);
        logger.info("Test01Job execute sleep end");
    } catch (InterruptedException e) {
        logger.error("Test01Job execute InterruptedException" + "_" + e.getMessage(), e);
    }
    logger.info("Test01Job execute success");

}
}























